package com.biostime.springdraw.common.wechat;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.utils.SpringContextHolder;
import com.biostime.springdraw.modules.sd.service.HttpRequestService;
import com.biostime.springdraw.modules.sys.dao.LogDao;
import com.biostime.springdraw.modules.sys.entity.Log;
import com.biostime.springdraw.modules.sys.entity.User;

public class Mama100Wechat {
	
	/**通过配置项获取域名**/
	public static String mama100Host = Global.getConfig("mama100Host");	
	
	public static String WechatInfo = "http://"+ mama100Host +"/weixin/api/user/weixin/";
	
	/**日志打印**/
	public static Logger logger=Logger.getLogger("mama-api"); 	
	
	public static class Api{
		
		private static LogDao logDao = SpringContextHolder.getBean(LogDao.class);
		
		/**
		 * 查询微信用户信息
		 * @author chenweicheng
		 * @version 2016-05-16
	     */		
		public static Map<String,Object> getWechatInfo(String openId){
			Map<String,Object> result = null;
			Log log  = new Log();
			String message = null;			
			
			try{
				
				/**新增日志记录**/
				log.setTitle("查询微信用户信息");
				log.setOutcome("1");
				log.setMethod("post");
				log.setRequestUri(Mama100Wechat.WechatInfo+openId);
				log.setRemoteAddr(openId);		
				log.setType("wechatInfo");
				log.setCreateBy(new User("1"));
				log.setCreateDate(new Date()); 	
				
				String entity = "openId:"+openId;
				
				//日志设置请求参数
				log.setUrlParam(entity);				
				
				byte[] b = HttpRequestService.sendGet(Mama100Wechat.WechatInfo+openId, null); 				
				String res = new String(b, "utf-8");
				
				result =toMap(res);
								
				if(result != null && (int)result.get("code") == 0){
					message = "调用成功";
					log.setOutcome("0");
				}else{
					message = "调用失败,失败原因:"+result.get("failureReason");
					log.setException(message); 
					log.setException(message); 
					logDao.insert(log);			
				}						
			}catch(Exception ex){
				result = new HashMap<String, Object>();
				result.put("code", "-1");
				result.put("desc", ex.getMessage());
				message = "调用失败,出现异常:"+ex.getMessage();
				log.setException(message); 
				log.setException(message); 
				logDao.insert(log);			
			}
			
			logger.info("----------"+log.getTitle()+":"+message+"----------"); 
			
			return result;
		}
	}
	
	/**
     * 将Json对象转换成Map
     * @param jsonObject
     * @return Map对象
     * @throws JSONException
     */
    public static Map<String,Object> toMap(String jsonString){

        JSONObject jsonObject = new JSONObject(jsonString);
        
        Map<String,Object> result = new HashMap<String,Object>();
        Iterator<?> iterator = jsonObject.keys();
        String key = null;
        
        while (iterator.hasNext()) {

            key = (String) iterator.next();
            try{
                result.put(key,  jsonObject.getInt(key));
            }catch(Exception e){
            	 result.put(key,  jsonObject.getString(key));
            }
            
        }
        return result;
    }			
}
